1 /***
2 * Redistribution and use of this software and associated documentation
3 * ("Software"), with or without modification, are permitted provided
4 * that the following conditions are met:
5 *
6 * 1. Redistributions of source code must retain copyright
7 * statements and notices. Redistributions must also contain a
8 * copy of this document.
9 *
10 * 2. Redistributions in binary form must reproduce the
11 * above copyright notice, this list of conditions and the
12 * following disclaimer in the documentation and/or other
13 * materials provided with the distribution.
14 *
15 * 3. The name "Exolab" must not be used to endorse or promote
16 * products derived from this Software without prior written
17 * permission of Exoffice Technologies. For written permission,
18 * please contact tma@netspace.net.au
19 *
20 * 4. Products derived from this Software may not be called "Exolab"
21 * nor may "Exolab" appear in their names without prior written
22 * permission of Exoffice Technologies. Exolab is a registered
23 * trademark of Exoffice Technologies.
24 *
25 * 5. Due credit should be given to the Exolab Project
26 * (http://www.exolab.org/).
27 *
28 * THIS SOFTWARE IS PROVIDED BY EXOFFICE TECHNOLOGIES AND CONTRIBUTORS
29 * ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT
30 * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
31 * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
32 * EXOFFICE TECHNOLOGIES OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
33 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
34 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
35 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
36 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
37 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
38 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
39 * OF THE POSSIBILITY OF SUCH DAMAGE.
40 *
41 * Copyright 2001-2004 (C) Exoffice Technologies Inc. All Rights Reserved.
42 *
43 * $Id: TestInvoker.java,v 1.3 2004/01/31 13:44:24 tanderson Exp $
44 */
45 package org.exolab.jmscts.core;
46
47 import junit.framework.Protectable;
48 import junit.framework.Test;
49 import junit.framework.TestResult;
50
51
52 /***
53 * Helper class to run a test case, for the given message type,
54 * delivery mode, messaging mode, and destination type.
55 * <p>
56 * Prior to running, it creates a new message of
57 * the specified type, and creates any destinations required by the test.
58 *
59 * @version $Revision: 1.3 $ $Date: 2004/01/31 13:44:24 $
60 * @author <a href="mailto:tma@netspace.net.au">Tim Anderson</a>
61 */
62 class TestInvoker implements Protectable {
63
64 /***
65 * The test case to run
66 */
67 private final Test _test;
68
69 /***
70 * The instance to collect test results in
71 */
72 private final TestResult _result;
73
74 /***
75 * The test context
76 */
77 private final TestContext _context;
78
79 /***
80 * The test filter. May be <code>null</code>.
81 */
82 private final TestFilter _filter;
83
84
85 /***
86 * Construct a new <code>TestInvoker</code>
87 *
88 * @param test the test to run. Must be an instance of <code>JmsTest</code>
89 * @param result the result of the test
90 * @param context the test context
91 * @param filter the test filter. May be <code>null</code>
92 */
93 public TestInvoker(Test test, TestResult result, TestContext context,
94 TestFilter filter) {
95 if (test == null) {
96 throw new IllegalArgumentException("Argument 'test' is null");
97 }
98 if (!(test instanceof JMSTest)) {
99 throw new IllegalArgumentException(
100 "Argument test must implement JMSTest");
101 }
102 if (result == null) {
103 throw new IllegalArgumentException("Argument 'result' is null");
104 }
105 if (context == null) {
106 throw new IllegalArgumentException("Argument 'context' is null");
107 }
108
109 _test = test;
110 _result = result;
111 _context = context;
112 _filter = filter;
113 }
114
115 /***
116 * Run the test
117 *
118 * @throws Exception for any error
119 */
120 public void protect() throws Exception {
121 JMSTest test = (JMSTest) _test;
122 TestContext context = _context;
123 boolean shared = true;
124
125 if (test instanceof TestRunner) {
126 ((TestRunner) test).setFilter(_filter);
127 }
128
129 TestContext connection = null;
130 TestContext session = null;
131
132 if (!test.share()) {
133 // test case cannot share resources, so allocate a new set
134 if (_context.getSession() != null) {
135 session = TestContextHelper.createSessionContext(_context);
136 connection = session.getParent();
137 context = session;
138 } else if (context.getConnection() != null) {
139 connection = TestContextHelper.createConnectionContext(
140 _context);
141 context = connection;
142 }
143 }
144
145 try {
146 setUp(test, context);
147 _test.run(_result);
148 tearDown(test, context);
149 } finally {
150 // if new contexts were allocated, close them
151 if (session != null) {
152 session.close();
153 }
154 if (connection != null) {
155 connection.close();
156 }
157 }
158 }
159
160 /***
161 * Returns the test
162 *
163 * @return the test
164 */
165 public Test getTest() {
166 return _test;
167 }
168
169 /***
170 * Returns the test context
171 *
172 * @return the test context
173 */
174 public TestContext getContext() {
175 return _context;
176 }
177
178 /***
179 * Setup the test
180 *
181 * @param test the test
182 * @param context the test context
183 * @throws Exception for any error
184 */
185 protected void setUp(JMSTest test, TestContext context) throws Exception {
186 // default implementation does nothing
187 }
188
189 /***
190 * Tear down the test
191 *
192 * @param test the test
193 * @param context the test context
194 * @throws Exception for any error
195 */
196 protected void tearDown(JMSTest test, TestContext context)
197 throws Exception {
198 // default implementation does nothing
199 }
200
201 }
This page was automatically generated by Maven